package Interview.yuanfudao;

import util.SwapUtil;

/**
 * B#$C%D 翻转字母顺序，符号顺序不变
 * @author leiwenkang
 * @date 2020/4/9
 */
public class Test01翻转字母顺序 {
    public static void main(String[] args) {
        String str = "B#$C%DE";
        System.out.println(reverseStr(str));
    }

    public static String reverseStr(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }

        char[] ch = str.toCharArray();
        int low = 0;
        int high = ch.length - 1;

        while (low < high) {
            while (low < high && !isLetter(ch[low])) {
                low++;
            }

            while (low < high && !isLetter(ch[high])) {
                high--;
            }

            if (low < high) {
                SwapUtil.swapCharArray(ch, low, high);
            }
            low++;
            high--;
        }
        return new String(ch);
    }


    private static boolean isLetter(char ch) {
        return  (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
    }


}
